﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DAL;

namespace WCFMINERP.BLL.OrderBll
{
    public class OrderInfoBll
    {
        public List<OrderInfo> getOrderInfo(int techId)
        {
            List<OrderInfo> PaperFaceModelsAll = new List<OrderInfo>();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<WorkOrderFlowList> list = db.WorkOrderFlowList.Where(p => p.Status == (int)OrderFlowListStatus.Useable && p.tb_Tech.TechId==techId).ToList();  
                
                foreach(WorkOrderFlowList item in list){

                    if (!item.orderInfoReference.IsLoaded)
                    {
                        item.orderInfoReference.Load();
                    }
                    
                    orderInfo model=db.orderInfo.FirstOrDefault(p=>p.OrderProductId==item.orderInfo.OrderProductId);
                    OrderInfo newModel = new OrderInfo();

                    newModel.OrderProductId = model.OrderProductId;
                    if (!model.Tb_OrderBagReference.IsLoaded)
                    {
                        model.Tb_OrderBagReference.Load();
                    }
                    newModel.OrderBagId = model.Tb_OrderBag.OrderBagId;
                    newModel.OrderProductCode = model.OrderProductCode;
                    newModel.OrderProductName = model.OrderProductName;
                    if (!model.Tb_ProductReference.IsLoaded)
                    {
                        model.Tb_ProductReference.Load();
                    }
                    newModel.ProductId = model.Tb_Product.ProuctId;
                    newModel.CtmName = model.CtmName;
                    newModel.CompleteTime = model.CompleteTime;
                    newModel.Status = model.status;
                    newModel.OrderPath = model.OrderPath;
                    newModel.OrilOrderPath = model.OrilOrderPath;
                    newModel.Width = Convert.ToSingle(model.Width);
                    newModel.Height = Convert.ToSingle(model.Height);
                    newModel.Remark = model.Remark;
                    newModel.IsEdit = model.IsEdit;
                    newModel.productNum = model.productNum;
                    newModel.PageNum = model.PageNum;
                    newModel.PrePriceCount = Convert.ToDecimal(model.PrePriceCount);
                    newModel.ParentProductId = model.ParentProductId;
                    newModel.ProductType = model.ProductType;
                    newModel.Priority = Convert.ToInt32(model.Priority);
                    newModel.isCount = Convert.ToInt32(model.isCount);
                    newModel.Area = model.Area;
                    newModel.SinglePageNum = model.SinglePageNum;
                    newModel.MultiPageNum = model.MultiPageNum;
                    newModel.OrderType = model.OrderType;
                    newModel.ComputerName = model.ComputerName;
                    newModel.OrilNum = model.OrilNum;

                    PaperFaceModelsAll.Add(newModel);
                }
                return PaperFaceModelsAll;
            }
        }


        /// <summary>
        /// 根据产品ID和岗位提交当前流程
        /// </summary>
        /// <param name="orderProductid"></param>
        /// <param name="techid"></param>
        /// <param name="EorrMess"></param>
        /// <returns></returns>
        public OrderInfo commitTech(string orderProductCode, int techid,int userid, out string EorrMess,out string imgpath)
        {
            EorrMess = "";
            imgpath = "";
            OrderInfo info=new OrderInfo();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (!db.orderInfo.Any(p => p.OrderProductCode == orderProductCode))
                {                   
                    EorrMess = "没有找到该条码！";
                    info.Commit=false;
                    return info;
                }
                orderInfo orderModels = db.orderInfo.FirstOrDefault(p => p.OrderProductCode == orderProductCode);

                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;

                List<OutProcessRecord> outProcessRecords = db.OutProcessRecord.Where(p => p.orderInfo.status == OrderStatus  && p.Status != Status && p.Status2 != (int)Status2).ToList();


                if (!orderModels.Tb_ProductReference.IsLoaded)
                {
                    orderModels.Tb_ProductReference.Load();
                }
                  if (!orderModels.Tb_OrderBagReference.IsLoaded)
                {
                    orderModels.Tb_OrderBagReference.Load();
                  }


                  info.photoCount = db.photoInfo.Where(p => p.orderInfo.OrderProductId == orderModels.OrderProductId).Count();
               

                info.CompleteTime=orderModels.CompleteTime;
                info.Area=orderModels.Area;
                info.ComputerName=orderModels.ComputerName;
                info.CtmName=orderModels.CtmName;
                info.Height=Convert.ToSingle( orderModels.Height);
                info.isCount=Convert.ToInt32(orderModels.isCount);
                info.IsEdit=orderModels.IsEdit;
                info.MultiPageNum=orderModels.MultiPageNum;
                              
                info.OrderBagId=orderModels.Tb_OrderBag.OrderBagId;
                info.OrderPath=orderModels.OrderPath;
                info.OrderProductCode=orderModels.OrderProductCode;
                info.OrderProductId=orderModels.OrderProductId;
                info.OrderProductName=orderModels.OrderProductName;
                info.OrderType=orderModels.OrderType;
                info.OrilNum=orderModels.OrilNum;
                info.OrilOrderPath=orderModels.OrilOrderPath;
                info.PageNum=orderModels.PageNum;
                info.ParentProductId=orderModels.ParentProductId;
                info.PrePriceCount = Convert.ToDecimal( orderModels.PrePriceCount);
                info.Priority= Convert.ToInt32( orderModels.Priority);
                
                info.ProductId=orderModels.Tb_Product.ProuctId;
                info.productNum=orderModels.productNum;
                info.ProductType=orderModels.ProductType;
                info.Remark=orderModels.Remark;
                info.SinglePageNum=orderModels.SinglePageNum;
                info.Status=orderModels.status;
                info.Width= Convert.ToSingle( orderModels.Width);

                imgpath = orderModels.OrderPath;

                if(info.prodinfo==null){
                    info.prodinfo=new ProductInfo();
                }
                info.prodinfo.AppendPrice=orderModels.Tb_Product.AppendPrice;
                info.prodinfo.AreaPrice=orderModels.Tb_Product.AreaPrice;
                info.prodinfo.Height=orderModels.Tb_Product.Height;
                info.prodinfo.PageNum=orderModels.Tb_Product.PageNum;
                info.prodinfo.ProductCode=orderModels.Tb_Product.ProductCode;
                info.prodinfo.ProductDes=orderModels.Tb_Product.ProductDes;
                info.prodinfo.ProductName=orderModels.Tb_Product.ProductName;
                info.prodinfo.ProuctId=orderModels.Tb_Product.ProuctId;
                info.prodinfo.Status=orderModels.Tb_Product.Status;
                info.prodinfo.UnitPrice=orderModels.Tb_Product.UnitPrice;
                info.prodinfo.Width=orderModels.Tb_Product.Width;
                
                info.prodinfo.MnemonicCode=orderModels.Tb_Product.mnemonicCode;

                

                if (info.orderBagInfo == null)
                    info.orderBagInfo = new OrderBagInfo();
                info.orderBagInfo.OrderbagName = orderModels.Tb_OrderBag.BagName;
                if (!orderModels.Tb_OrderBag.ShopReference.IsLoaded)
                {
                    orderModels.Tb_OrderBag.ShopReference.Load();
                }
                info.orderBagInfo.ShopName = orderModels.Tb_OrderBag.Shop.shopName;
                info.orderBagInfo.ShopId = orderModels.Tb_OrderBag.Shop.ShopId;
                info.FacthTime = orderModels.Tb_OrderBag.FetchTime;

                Tb_Product productType = orderModels.Tb_Product;

                productType.Tb_OutShop_ProductCategory.Load();

                if (outProcessRecords.Count > 0)
                {
                    switch (outProcessRecords[0].Status)
                    {
                        case (int)OutProcessRecordStatus.Outing:
                            {
                                switch (outProcessRecords[0].Status2)
                                {
                                    case (int)OutProcessRecordStatus2.Doing:
                                        info.strStatus = "正在生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Error:
                                        info.strStatus = "暂停生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Complete:
                                        info.strStatus = "生产完成";
                                        break;
                                    default:
                                        info.strStatus = "等待接收";
                                        break;
                                }
                            }
                            break;
                        case (int)OutProcessRecordStatus.InGrid:
                            {
                                info.strStatus = "未打印(在格子中)";
                            }
                            break;
                        case (int)OutProcessRecordStatus.OutWaitFenDui:
                            {
                                info.strStatus = "未打印(等待分堆)";
                            }

                            break;
                    }
                }

                if (!orderModels.WorkOrderFlowList.IsLoaded)   
                {
                    orderModels.WorkOrderFlowList.Load();//获取当前所有流程
                    foreach (var wo in orderModels.WorkOrderFlowList)
                    {
                        if (!wo.tb_TechReference.IsLoaded)
                        {
                            wo.tb_TechReference.Load();  //加载岗位
                        }
                    }
                }

                if (!orderModels.WorkOrderFlowList.Any(p => p.tb_Tech.TechId == techid))
                {
                    EorrMess = "该产品不存在此流程！";
                    info.Commit=false;
                    return info;
                }

                WorkOrderFlowList flowmodel = orderModels.WorkOrderFlowList.FirstOrDefault(p => p.tb_Tech.TechId == techid);
                if (flowmodel.Status != (int)OrderFlowListStatus.Useable)
                {
                    WorkOrderFlowList model = orderModels.WorkOrderFlowList.FirstOrDefault(p => p.Status == (int)OrderFlowListStatus.Useable);
                    if (model != null)
                    {
                        EorrMess = "当前流程在：" + model.tb_Tech.TechName;
                        info.Commit = false;
                        return info;
                    }
                    else
                    {
                        EorrMess = "流程已经结束！";
                        info.Commit = false;
                        return info;
                    }
                }

                flowmodel.UserId = userid;
                flowmodel.Status = (int)OrderFlowListStatus.Complete;   //改为已经提交

                //激活下一流程
                WorkOrderFlowList flowmodel2 = orderModels.WorkOrderFlowList.Where(p => p.WorkOrder > flowmodel.WorkOrder).OrderBy(p => p.WorkOrder).FirstOrDefault();
                if (flowmodel2 != null)
                {
                    flowmodel2.Status = (int)OrderFlowListStatus.Useable;  //可操作
                }
                try
                {
                    db.SaveChanges();
                    EorrMess = "提交成功！";
                    info.Commit = true;
                    return info;
                }
                catch (Exception ex)
                {
                    EorrMess = "提交失败！";
                    info.Commit = false;
                }                
            }
            return info ;
        }

        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="orderProductCode"></param>
        /// <param name="EorrMess"></param>
        /// <returns></returns>
        public OrderInfo SercahProduct(string orderProductCode, out string EorrMess)
        {
            EorrMess = "";           
            OrderInfo info = new OrderInfo();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (!db.orderInfo.Any(p => p.OrderProductCode == orderProductCode))
                {
                    EorrMess = "没有找到该条码！";
                    info.Commit = false;
                    return info;
                }
                orderInfo orderModels = db.orderInfo.FirstOrDefault(p => p.OrderProductCode == orderProductCode);

                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;

                List<OutProcessRecord> outProcessRecords = db.OutProcessRecord.Where(p => p.orderInfo.status == OrderStatus && p.Status != Status && p.Status2 != (int)Status2).ToList();
                
                if (!orderModels.Tb_ProductReference.IsLoaded)
                {
                    orderModels.Tb_ProductReference.Load();
                }
                if (!orderModels.Tb_OrderBagReference.IsLoaded)
                {
                    orderModels.Tb_OrderBagReference.Load();
                }

                info.photoCount = db.photoInfo.Where(p => p.orderInfo.OrderProductId == orderModels.OrderProductId).Count();
                

                info.CompleteTime = orderModels.CompleteTime;
                info.Area = orderModels.Area;
                info.ComputerName = orderModels.ComputerName;
                info.CtmName = orderModels.CtmName;
                info.Height = Convert.ToSingle(orderModels.Height);
                info.isCount = Convert.ToInt32(orderModels.isCount);
                info.IsEdit = orderModels.IsEdit;
                info.MultiPageNum = orderModels.MultiPageNum;

                info.OrderBagId = orderModels.Tb_OrderBag.OrderBagId;
                info.OrderPath = orderModels.OrderPath;
                info.OrderProductCode = orderModels.OrderProductCode;
                info.OrderProductId = orderModels.OrderProductId;
                info.OrderProductName = orderModels.OrderProductName;
                info.OrderType = orderModels.OrderType;
                info.OrilNum = orderModels.OrilNum;
                info.OrilOrderPath = orderModels.OrilOrderPath;
                info.PageNum = orderModels.PageNum;
                info.ParentProductId = orderModels.ParentProductId;
                info.PrePriceCount = Convert.ToDecimal(orderModels.PrePriceCount);
                info.Priority = Convert.ToInt32(orderModels.Priority);

                info.ProductId = orderModels.Tb_Product.ProuctId;
                info.productNum = orderModels.productNum;
                info.ProductType = orderModels.ProductType;
                info.Remark = orderModels.Remark;
                info.SinglePageNum = orderModels.SinglePageNum;
                info.Status = orderModels.status;
                info.Width = Convert.ToSingle(orderModels.Width);
                
               
                
                if (info.prodinfo == null)
                {
                    info.prodinfo = new ProductInfo();
                }
                info.prodinfo.AppendPrice = orderModels.Tb_Product.AppendPrice;
                info.prodinfo.AreaPrice = orderModels.Tb_Product.AreaPrice;
                info.prodinfo.Height = orderModels.Tb_Product.Height;
                info.prodinfo.PageNum = orderModels.Tb_Product.PageNum;
                info.prodinfo.ProductCode = orderModels.Tb_Product.ProductCode;
                info.prodinfo.ProductDes = orderModels.Tb_Product.ProductDes;
                info.prodinfo.ProductName = orderModels.Tb_Product.ProductName;
                info.prodinfo.ProuctId = orderModels.Tb_Product.ProuctId;
                info.prodinfo.Status = orderModels.Tb_Product.Status;
                info.prodinfo.UnitPrice = orderModels.Tb_Product.UnitPrice;
                info.prodinfo.Width = orderModels.Tb_Product.Width;

                info.prodinfo.MnemonicCode = orderModels.Tb_Product.mnemonicCode;



                if (info.orderBagInfo == null)
                    info.orderBagInfo = new OrderBagInfo();
                info.orderBagInfo.OrderbagName = orderModels.Tb_OrderBag.BagName;
                if (!orderModels.Tb_OrderBag.ShopReference.IsLoaded)
                {
                    orderModels.Tb_OrderBag.ShopReference.Load();
                }
                info.orderBagInfo.ShopName = orderModels.Tb_OrderBag.Shop.shopName;
                info.orderBagInfo.ShopId = orderModels.Tb_OrderBag.Shop.ShopId;
                info.FacthTime = orderModels.Tb_OrderBag.FetchTime;

                Tb_Product productType = orderModels.Tb_Product;

                productType.Tb_OutShop_ProductCategory.Load();

                if (outProcessRecords.Count > 0)
                {
                    switch (outProcessRecords[0].Status)
                    {
                        case (int)OutProcessRecordStatus.Outing:
                            {
                                switch (outProcessRecords[0].Status2)
                                {
                                    case (int)OutProcessRecordStatus2.Doing:
                                        info.strStatus = "正在生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Error:
                                        info.strStatus = "暂停生产";
                                        break;
                                    case (int)OutProcessRecordStatus2.Complete:
                                        info.strStatus = "生产完成";
                                        break;
                                    default:
                                        info.strStatus = "等待接收";
                                        break;
                                }
                            }
                            break;
                        case (int)OutProcessRecordStatus.InGrid:
                            {
                                info.strStatus = "未打印(在格子中)";
                            }
                            break;
                        case (int)OutProcessRecordStatus.OutWaitFenDui:
                            {
                                info.strStatus = "未打印(等待分堆)";
                            }

                            break;
                    }
                }
            }
            return info;
        }
    }
}
